import { defineStore} from 'pinia'
import worthApi, { type resData,type SwiperData } from '@/api/worth'
import pubuApi, { type pubuData } from "@/api/pubu"
export interface stateDate{
    worthData:SwiperData[]
    pubuallData:pubuData
}
export const useWorthStore = defineStore('worth', {
  state(): stateDate {
    return {
      worthData: [],
      pubuallData:{
        "hasMore":true,
        "result":[],
        "extra":null
      }
    }
  },
  actions: {
    async getResData(){
      try {
        // 发送api请求
        const res = await worthApi.getWorthData()
        this.worthData = res.navList
      } catch (error) {
        return Promise.reject(error)
      }
    },
    //瀑布流数据
    async getpubuData(page: number, size: number){
      try {
        // 发送api请求
        const res = await pubuApi.getpubuData(page,size)
        this.pubuallData.result= this.pubuallData.result.concat(res.result)
      } catch (error) {
        return Promise.reject(error)
        
      }
    }
  },
    getters: {
      setResData(state){
        return state.worthData.reduce(
          (prev,item:SwiperData,index)=>(index%2==0?prev.push([item]):prev[prev.length-1].push(item))&&prev,[]as any
        )
      },
      setpubuallData(state){
        return state.pubuallData.result.map((item)=>[...item.topics,item.look]).flat()
      }
    }
})
